package com.sie.filter;

import cn.hutool.json.JSONUtil;
import com.sie.entity.ViewEmp;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.time.LocalDateTime;

/**
 * token合法验证过滤器
 */
@WebFilter(urlPatterns = "/**")
@Component
public class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) {
//        获取请求头以及响应头
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
//        获取访问路径
        String requestURI = request.getRequestURI();
        try {
//            放行过滤路径
            if (requestURI.equals("/out") || requestURI.equals("/login") || requestURI.contains("swagger") || requestURI.equals("/") || requestURI.equals("/v2/api-docs") || requestURI.equals("/csrf")) {
                filterChain.doFilter(servletRequest, servletResponse);
            } else {
//                获取token
                String token = request.getHeader("token");
//                效验token是否存在,不存在则403
                if (token == null || token.equals("")) {
                    response.setStatus(403);
                } else {
//                    获取系统当前时间
                    String time = LocalDateTime.now().toString();
//                    截取时间格式yyyy-MM-dd,HH:mm:ss
                    String[] s = time.split("T");
//                    解析令牌发放时间
                    String time2 = token.split("TIME_")[1];
//                    截取时间格式yyyy-MM-dd,HH:mm:ss
                    String[] s1 = time2.split("T");
//                    检测是否为当天发放的token
                    if (s[0].compareTo(s1[0]) == 0) {
//                        获取HH时间戳
                        String[] split = s[1].split(":");
                        String[] split1 = s1[1].split(":");
//                        检测是否为同一个时
                        if (Integer.parseInt(split[0]) - Integer.parseInt(split1[0]) == 0) {
//                            检测是否在30m之内
                            if (Integer.parseInt(split[1]) - Integer.parseInt(split1[1]) < 30) {
//                                token合法,放行
                                packages(servletRequest, servletResponse, filterChain, token);
                            } else {
                                response.setStatus(403);
                            }
//                            检验是否不在同一个时且大于一
                        } else if (Integer.parseInt(split[0]) - Integer.parseInt(split1[0]) == 1) {
//                            检测两个时间戳是否超过30m
                            if ((Integer.parseInt(split[1]) + 60 - Integer.parseInt(split1[1]) < 30)) {
//                                token合法
                                packages(servletRequest, servletResponse, filterChain, token);
                            } else {
                                response.setStatus(403);
                            }
                        } else {
                            response.setStatus(403);
                        }
                    } else {
                        response.setStatus(403);
                    }
                }
            }
        } catch (Exception e) {
            response.setStatus(403);
        }
    }

    private void packages(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain, String token) throws IOException, ServletException {
//        获取登录用户信息
        String user = token.split("USER_")[1];
//        解码
        String user2 = URLDecoder.decode(user, "utf-8");
        String u = "";
//        将用户数据处理成JSON
        for (String s2 : user2.split("\\\\")) {
            u = u + s2;
        }
//        将JSON放到request域
        servletRequest.setAttribute("user", JSONUtil.toBean(u, ViewEmp.class));
        filterChain.doFilter(servletRequest, servletResponse);
    }

}
